package com.s2m;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

public class DiseaseUtil {
	public static void main(String[] args) throws IOException {
		List<String> resultTemp = readFile02("DISEASES.txt");
		List<String> result = new ArrayList<>();
		for (String rt : resultTemp) {
			String[] a = rt.replace("\"", "").split("\t");
			// 判断别名是否为空
			if (a.length == 3) {
				Set<String> temp = new HashSet<>();
				temp.add(a[1].trim());
				for (String tt : a[2].split("\\|\\|")) {
					temp.add(tt.trim());
				}
				if (temp.size() > 1) {
					result.add(String.join(",", temp));
				}
			}
		}
		String fileName = "synonym.txt";

		BufferedWriter out = null;
		try {
			if (result != null && !result.isEmpty() && StringUtils.isNotEmpty(fileName)) {
				File file = new File(fileName);
				if (!file.exists()) {
					file.createNewFile();
				}
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
				for (String info : result) {
					out.write(info);
					out.newLine();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static List<String> readFile02(String path) throws IOException {
		// 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
		List<String> list = new ArrayList<String>();
		FileInputStream fis = new FileInputStream(path);
		// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
		BufferedReader br = new BufferedReader(isr);
		String line = "";
		while ((line = br.readLine()) != null) {
			// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
			if (line.lastIndexOf("---") < 0) {
				list.add(line);
			}
		}
		br.close();
		isr.close();
		fis.close();
		return list;
	}
}
